package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.douwong.student.R;
import com.douwong.student.activity.ClassTestActivity;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.interfaces.ClassTestInterface;
import com.douwong.student.model.ResTestProblemsInfo;
import com.douwong.student.model.TestInfos;
import com.douwong.student.popuwindow.ShowSubjectDetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.CharPopuwindow;
import com.douwong.student.view.MTextView;

/**
 * @autor yichao
 * @time 2017/4/516:06
 * @des 填空题
 */
@SuppressLint("ValidFragment")
public class FillFragment extends BaseFragment implements ClassTestInterface {

    private View mContenView;
    private ImageView mIvHead;
    private MTextView mMtTopic;
    private LinearLayout mLlTitleRoot,mBlank_ll;
    private ClassTestActivity mClassTestActivity;
    private ResTestProblemsInfo.TopicItemBean mTopicItemBean;
    private int mChildCount;
    private String lastAnswerStr="";
    private  ImageView mImg;
    private ShowSubjectDetailPopuWindow mPoDetailPopu;


    public FillFragment(ClassTestActivity classTestActivity, ResTestProblemsInfo.TopicItemBean topicItemBean) {
        super();
        mClassTestActivity = classTestActivity;
        mTopicItemBean = topicItemBean;
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {

        mContenView = UIUtils.inflate(getActivity(), R.layout.fill_fragment);
        mIvHead = (ImageView) mContenView.findViewById(R.id.iv_answer_show_pic);
        mMtTopic = (MTextView) mContenView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContenView.findViewById(R.id.ll_choicefragme_title_root);
        mBlank_ll = (LinearLayout) mContenView.findViewById(R.id.blank_ll);

        return mContenView;
    }

    @Override
    protected void initData() {
        super.initData();
        ImagFilterUtil.filterMTImage(mMtTopic, mTopicItemBean.getBody().trim());
        //判读是否显示图片
        boolean showIcon = ImagFilterUtil.isShowIcon(mTopicItemBean);

        if (showIcon) {
            mIvHead.setVisibility(View.VISIBLE);
        }

        for (int i = 0; i < mTopicItemBean.getAnswers().size(); i++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            lp.topMargin = 15;
            LinearLayout mainLayout = new LinearLayout(getActivity());
            mainLayout.setOrientation(LinearLayout.HORIZONTAL);
            mainLayout.setGravity(Gravity.CENTER_VERTICAL);
            mainLayout.setLayoutParams(lp);
            final EditText editText = new EditText(getActivity());
            editText.setSingleLine();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.width = 0;
            params.height = UIUtils.dip2px(60);
            params.weight = 3;
            editText.setLayoutParams(params);
            mainLayout.addView(editText);
            mBlank_ll.addView(mainLayout);

            //符号弹窗
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            mImg = new ImageView(getActivity());
            param.width = 0;
            param.height =60;
            param.weight = 1;
            param.leftMargin = 5;
            mImg.setLayoutParams(param);
            mImg.setImageResource(R.mipmap.char_img);
            mainLayout.addView(mImg);
            mImg.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    CharPopuwindow popu = new CharPopuwindow(mClassTestActivity, editText);
                    // popu.showAsDropDown(editText);
                    popu.showAtLocation(v, Gravity.CENTER, 0, 0);
                }
            });
        }
    }

    @Override
    protected void initEvent() {
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                mPoDetailPopu = new ShowSubjectDetailPopuWindow(getActivity(),
                        mTopicItemBean  );
                mPoDetailPopu.showAtLocation(getView(), Gravity.CENTER, 0, 0);

            }
        });



    }

    @Override
    public TestInfos getAnswerInfo() {
        mChildCount = mBlank_ll.getChildCount();
        String[] optionStrArr = new String[mChildCount];
        //计算得分
        double sore = 0;
        for (int i = 0; i < mChildCount; i++) {
            LinearLayout linearLayout = (LinearLayout) mBlank_ll.getChildAt(i);
            EditText childAt= (EditText) linearLayout.getChildAt(0);
            String trim = childAt.getText().toString().trim();
            lastAnswerStr+=trim;

            if (trim.equals("") || !mTopicItemBean.getAnswers().contains(trim)||!trim.equals(mTopicItemBean.getAnswers().get(i))) {
                sore = 0;
                break;
            } else {
                sore = mTopicItemBean.getScore();
            }
        }
        //得到答案
        for (int i = 0; i < mChildCount; i++) {
            LinearLayout linearLayout = (LinearLayout) mBlank_ll.getChildAt(i);
            EditText childAt= (EditText) linearLayout.getChildAt(0);
            String trim = childAt.getText().toString().trim();
            optionStrArr[i] = trim;
        }
        //判断是否没做
        if (lastAnswerStr.equals("")||lastAnswerStr.length()==0){
            BaseApplication.noDoTestSum++;
        }
        return new TestInfos(mTopicItemBean.getId(), sore, optionStrArr);
    }

    @Override
    public void setCommitStatue() {
        for (int i = 0; i < mChildCount; i++) {
            LinearLayout linearLayout = (LinearLayout) mBlank_ll.getChildAt(i);
            EditText eTAnswer = (EditText) linearLayout.getChildAt(0);
             ImageView imageView= (ImageView) linearLayout.getChildAt(1);
            eTAnswer.setEnabled(false);
            imageView.setFocusable(false);
            imageView.setEnabled(false);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPoDetailPopu!=null && mPoDetailPopu.isShowing()){
            mPoDetailPopu.dismiss();
        }
    }
}
